package com.bluebench.bigul.domain.graphics.simple.polygon;

import java.util.ArrayList;
import java.util.List;

import com.bluebench.bigul.domain.graphics.Graphic;
import com.bluebench.bigul.domain.graphics.Line;
import com.bluebench.bigul.domain.graphics.Point;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.PdfContentByte;

public class Rectangle extends Quadilateral{

	
	public Rectangle(PdfContentByte canvas) {
		super(canvas);
	}

	@Override
	public Polygon build(Point start, List<Float> sides) {
				
		float x = start.getX();
		float y = start.getY();
		
		Float side1 = sides.get(0);
		Float side2 = sides.get(1);
		Float side3 = null;
		Float side4 = null; 
		if(sides.size()!=4){
			side3 = side1;
			side4 = side2;
		}else{
			side3 = sides.get(2);
			side4 = sides.get(3);
		}
		
		this.vertices.clear();
		addVertex(new Point(x,y));
		addVertex(new Point(x + side1,y));
		addVertex(new Point(x + side1,y + side2));		
		addVertex(new Point(x+side1-side3,y + side2));
		
		this.sides.clear();
		addSide(new Line(side1));
	    addSide(new Line(side2));
	    addSide(new Line(side3));
	    addSide(new Line(side4));
	   
	    if(validate()){
			canvas.moveTo(x, y);   			
		    canvas.lineTo(x + side1, y);		    
		    canvas.lineTo(x + side1, y + side2);		    
		    canvas.lineTo(x+side1-side3, y + side2);		   
	    }
	    	    
	    return this;
	}
	
	public Graphic build(Point start, List<Float> sides, float rotation){
		
		float x0 = start.getX();
		float y0 = start.getY();
		
		float horz = sides.get(0);
		float vert = sides.get(1);
		
		Point pivot = new Point(x0+(horz/2),y0+(vert/2));
		Point p0_rotate =  getRotation(start, pivot, rotation);
		
		float x1 = x0 +horz;
		float y1 = y0;
		Point p1_rotate = getRotation(new Point(x1,y1), pivot, rotation);
		
		float x2 = x1;
		float y2 = y1 + vert;
		Point p2_rotate = getRotation(new Point(x2,y2), pivot, rotation);
		
		float x3 = x0;
		float y3 = y2;
		Point p3_rotate = getRotation(new Point(x3,y3), pivot, rotation);
		
		addVertex(p0_rotate);
		addVertex(p1_rotate);
		addVertex(p2_rotate);
		addVertex(p3_rotate);
		
		build();		
		
		return this;
		
	}
	
	@Override
	public Graphic concentric(float distance, boolean in) {
		float x = vertices.get(0).getX();
		float y = vertices.get(0).getY();
		Point start=null;
		float horz=0;
		float vert=0;
		List<Float> sidesFloat = new ArrayList<Float>();
		if(in){
			start = new Point(x+distance,y+distance);
			//TODO : validate that lenght > 2d
			horz = sides.get(0).getLength() - (2*distance);
			vert = sides.get(1).getLength() - (2*distance);
			sidesFloat.add(horz);
			sidesFloat.add(vert);
		}else{
			start = new Point(x-distance,y-distance);
			//TODO : validate that lenght > 2d
			horz = sides.get(0).getLength() + (2*distance);
			vert = sides.get(1).getLength() + (2*distance);
			sidesFloat.add(horz);
			sidesFloat.add(vert);
		}
		return build(start,sidesFloat);
	}

	@Override
	protected boolean validate() {
		
		if(vertices!=null && (!vertices.isEmpty()) && vertices.size()==4){
			
			Point A = vertices.get(0);
			Point B = vertices.get(1);
			Point C = vertices.get(2);
			Point D = vertices.get(3);
			
			double s1Sqr = Math.pow((A.getX()-B.getX()),2) + Math.pow((A.getY()-B.getY()),2);
			double s2Sqr = Math.pow((B.getX()-C.getX()),2) + Math.pow((B.getY()-C.getY()),2);
			double s3Sqr = Math.pow((C.getX()-D.getX()),2) + Math.pow((C.getY()-D.getY()),2);
			double s4Sqr = Math.pow((D.getX()-A.getX()),2) + Math.pow((D.getY()-A.getY()),2);
						
			double diagonal1Square = Math.pow((Math.ceil(A.getX())-Math.ceil(C.getX())),2) + Math.pow(( Math.ceil(A.getY())-Math.ceil(C.getY())),2);
			double diagonal2Square = Math.pow((Math.ceil(D.getX())-Math.ceil(B.getX())),2) +  Math.pow((Math.ceil(D.getY())-Math.ceil(B.getY())),2);			
			 
			if(s1Sqr==s3Sqr && s2Sqr==s4Sqr && diagonal1Square==diagonal2Square){
				return true;
			}else{
				System.out.println("validation fails");
				return false;
			}
			
		}else if(sides!=null && sides.size()==4){
			
			if(sides.get(0).getLength()==sides.get(1).getLength() && sides.get(1).getLength()==sides.get(2).getLength() && sides.get(2).getLength()==sides.get(3).getLength() && sides.get(3).getLength()==sides.get(1).getLength()){
			  return true;
			}			
			return false;
			
		}else{			
			return false;
			
		}
	}	

	@Override
	public Graphic copy(float distance, int direction) {
		// TODO Auto-generated method stub
		return null;
	}	

}
